//
//  SISSProgressBottomView.m
//  SISSAPP
//
//  Created by 优房网 on 2017/7/6.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSProgressBottomView.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface SISSProgressBottomView ()

/* iconImageView */
@property (weak, nonatomic) UIImageView *iconImageView;
/* 中介姓名label */
@property (weak, nonatomic) UILabel *nameLabel;
/* 电话按钮 */
@property (weak, nonatomic) UIButton *telephoneButton;
/* 在线咨询按钮 */
@property (weak, nonatomic) UIButton *onlineConsultButton;

@end
@implementation SISSProgressBottomView

- (void)setIconUrlStr:(NSString *)iconUrlStr {
    _iconUrlStr = iconUrlStr;
    
    [self.iconImageView sd_setImageWithURL:[NSURL URLWithString:iconUrlStr] placeholderImage:[UIImage imageNamed:@"头像"]];
}

- (void)setName:(NSString *)name {
    _name = name;
    self.nameLabel.text = name;
}

- (void)setBusinessStatus:(NSInteger)businessStatus {
    _businessStatus = businessStatus;
    self.onlineConsultButton.hidden = businessStatus == 2;
    if (businessStatus == 2) {
        [self.telephoneButton setTitle:@"对方拒绝接收" forState:UIControlStateNormal];
        [self.telephoneButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(SCREEN_WIDTH * 0.6));
        }];
        [self.onlineConsultButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@0.01);
        }];
    } else {
        [self.telephoneButton setTitle:@"电话" forState:UIControlStateNormal];
        [self.telephoneButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(SCREEN_WIDTH * 0.3));
        }];
        [self.onlineConsultButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(@(SCREEN_WIDTH * 0.3 * 0.9));
        }];
    }
}

- (void)initUI {
    [super initUI];
    
    self.backgroundColor = COLOR(0x343848);
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"头像"];
    imageView.layer.cornerRadius = 20;
    imageView.layer.masksToBounds = YES;
    [self addSubview:imageView];
    self.iconImageView = imageView;
    
    UILabel *nameLabel = [[UILabel alloc] init];
    nameLabel.textColor = [UIColor whiteColor];
    nameLabel.text = @"姓名";
    [self addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UIButton *telButton = [UIButton buttonWithType:UIButtonTypeCustom];
    telButton.tag = 1;
    [telButton setTitle:@"电话" forState:UIControlStateNormal];
    [telButton setTitleColor:MAIN_TEXT_COLOR forState:UIControlStateNormal];
    [telButton setBackgroundColor:COLOR(0xC7DE21)];
    [telButton addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:telButton];
    self.telephoneButton = telButton;
    
    UIButton *consultButton = [UIButton buttonWithType:UIButtonTypeCustom];
    consultButton.tag = 2;
    [consultButton setTitle:@"在线咨询" forState:UIControlStateNormal];
    [consultButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [consultButton setBackgroundColor:MAIN_COLOR];
    [consultButton addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:consultButton];
    self.onlineConsultButton = consultButton;
    
    UIButton *coverView = [[UIButton alloc] init];
    coverView.tag = 0;
    [coverView addTarget:self action:@selector(buttonClickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:coverView];
    
    WeakSelf
    // 自动布局
    CGFloat margin = 8.0;
    CGFloat bottomButtonWidth = SCREEN_WIDTH * 0.3;
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(weakSelf);
        make.left.equalTo(weakSelf).offset(2 * margin);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView.mas_right).offset(2 * margin);
        make.centerY.equalTo(imageView);
    }];
    
    [telButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(consultButton.mas_left);
        make.top.bottom.equalTo(weakSelf);
        make.width.mas_equalTo(bottomButtonWidth * 0.9);
    }];
    
    [consultButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.bottom.equalTo(weakSelf);
        make.width.mas_equalTo(bottomButtonWidth);
    }];
    
    [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(imageView);
        make.right.equalTo(nameLabel);
        make.top.bottom.equalTo(weakSelf);
    }];
}


- (void)buttonClickAction:(UIButton *)button {
    
    if ([self.delegate respondsToSelector:@selector(clickBottomButtonWithIndex:)]) {
        [self.delegate clickBottomButtonWithIndex:button.tag];
    }
}

@end
